﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Cherry.Agile.FileHandling;
using System.IO;

namespace Cherry.Agile.RpcService.ExtProcessor
{
    /// <summary>
    /// 请求数据拦截记录器。
    /// </summary>
    public class RpcRequestCapture2 : IServiceExtentProcessor
    {
        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="context"></param>
        public void ProcessOnStarting(ProcessContext context)
        {
            string data = ByteToString(context.Reader.Buffer);
            string fileName = string.Format(@"\Remp\RpcSerivce\Request2\E{0:yyMMddHHmmss}_{1}.txt",
                DateTime.Now,
                context.GetHashCode()
                );
            using (Stream stream = FileManager.OpenFile(fileName, FileMode.OpenOrCreate))
            {
                stream.Append(data);
            };
        }



        /// <summary>
        /// 结束处理
        /// </summary>
        /// <param name="context"></param>
        public void ProcessOnEnding(ProcessContext context)
        {
            ;
        }

        /// <summary>
        /// 跟踪异常信息
        /// </summary>
        /// <param name="ex">异常信息</param>
        public void ProcessTrace(Exception ex, ProcessContext context)
        {

        }

        /// <summary>
        /// 跟踪信息输出
        /// </summary>
        /// <param name="traceMessage"></param>
        public void ProcessTrace(string traceMessage, ProcessContext context)
        {

        }

        /// <summary>
        /// 字节传换成字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string ByteToString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(1024);
            foreach (byte b in data)
                sb.Append(b.ToString("X2"));
            return sb.ToString();
        }
    }
}
